package com.shengcheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shengcheng.entity.BaseCategory2;
import com.shengcheng.entity.BaseCategory3;
import com.shengcheng.mapper.BaseCategory3Mapper;
import com.shengcheng.service.BaseCategory2Service;
import com.shengcheng.service.BaseCategory3Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 三级分类表 服务实现类
 * </p>
 *
 * @author bch
 * @since 2024-06-25
 */
@Service
public class BaseCategory3ServiceImpl extends ServiceImpl<BaseCategory3Mapper, BaseCategory3> implements BaseCategory3Service {


    @Autowired
    private BaseCategory2Service baseCategory2Service;
    @Autowired
    private BaseCategory3Service baseCategory3Service;

    @Override
    public List<BaseCategory3> getCategory3ListByCategory1Id(Long category1Id) {
        //根据一级分类id查询二级分类
        LambdaQueryWrapper<BaseCategory2> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategory2::getCategory1Id,category1Id)
                .select(BaseCategory2::getId)
                .orderByDesc(BaseCategory2::getOrderNum);

        List<BaseCategory2> category2s = baseCategory2Service.list(queryWrapper);
        //根据二级分类id查询三级分类id
        List<Long> category2IdList = category2s.stream().map(BaseCategory2::getId).collect(Collectors.toList());

        LambdaQueryWrapper<BaseCategory3> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(BaseCategory3::getCategory2Id,category2IdList)
                .eq(BaseCategory3::getIsTop,1)
                .last("limit 8");

        List<BaseCategory3> baseCategory3s = baseCategory3Service.list(wrapper);
        return baseCategory3s;
    }
}
